<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 左侧欢迎区域 -->
      <div class="welcome-section">
        <div class="welcome-content">
          <div class="logo">油柑博客</div>
          <h2 class="welcome-title">欢迎回来</h2>
          <p class="welcome-subtitle">分享知识，连接世界</p>
          <div class="welcome-image">
            <img src="https://img.freepik.com/free-vector/tablet-login-concept-illustration_114360-7963.jpg" alt="Welcome image">
          </div>
        </div>
      </div>
      
      <!-- 右侧登录表单 -->
      <div class="form-section">
        <div class="form-header">
          <h2>用户登录</h2>
          <p>请输入您的账号和密码</p>
        </div>
        
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              prefix-icon="User"
              clearable
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="密码"
              prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <div class="form-options">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            <a href="#" class="forgot-link">忘记密码?</a>
          </div>
          
          <el-button
            type="primary"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
          
          <div class="register-link">
            还没有账号? <router-link to="/register">立即注册</router-link>
          </div>
          
          <div class="social-login">
            <div class="divider">
              <span>其他登录方式</span>
            </div>
            <div class="social-icons">
              <el-button circle class="social-icon wechat">
                <el-icon><ChatDotRound /></el-icon>
              </el-button>
              <el-button circle class="social-icon weibo">
                <el-icon><Share /></el-icon>
              </el-button>
              <el-button circle class="social-icon github">
                <el-icon><Platform /></el-icon>
              </el-button>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, ChatDotRound, Share, Platform } from '@element-plus/icons-vue'
import { login } from '@/api/auth'

const router = useRouter()
const loading = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

const handleLogin = async () => {
  try {
    loading.value = true
    const res = await login({
      username: loginForm.username,
      password: loginForm.password
    })
    
    // 保存用户信息到本地存储
    localStorage.setItem('user', JSON.stringify(res.data))
    ElMessage.success('登录成功')
    
    // 如果有重定向路径，则跳转到对应的页面，否则默认跳转到首页
    const redirect = router.currentRoute.value.query.redirect || '/'
    router.push(redirect)
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.response?.data?.message || '登录失败，请稍后再试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
}

.login-box {
  width: 900px;
  background-color: white;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

/* 左侧欢迎区域 */
.welcome-section {
  flex: 1;
  background: linear-gradient(135deg, #6ca3ef 0%, #4f88e3 100%);
  color: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.welcome-content {
  max-width: 350px;
  margin: 0 auto;
}

.logo {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 60px;
}

.welcome-title {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 16px;
}

.welcome-subtitle {
  font-size: 16px;
  margin-bottom: 40px;
  opacity: 0.8;
}

.welcome-image {
  max-width: 100%;
  margin-top: 20px;
}

.welcome-image img {
  width: 100%;
}

/* 右侧表单区域 */
.form-section {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
}

.form-header p {
  color: #909399;
}

.login-form {
  max-width: 340px;
  margin: 0 auto;
}

.login-form :deep(.el-input__wrapper) {
  padding: 12px 15px;
  border-radius: 8px;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-link {
  color: #909399;
  font-size: 14px;
  text-decoration: none;
}

.forgot-link:hover {
  color: #409EFF;
  text-decoration: underline;
}

.login-button {
  width: 100%;
  padding: 12px 20px;
  font-size: 16px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.register-link {
  text-align: center;
  margin-bottom: 30px;
  color: #606266;
  font-size: 14px;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}

.social-login {
  margin-top: 20px;
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
}

.divider:before, 
.divider:after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #ebeef5;
}

.divider span {
  padding: 0 15px;
  color: #909399;
  font-size: 14px;
}

.social-icons {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 20px;
}

.social-icon {
  transition: transform 0.3s;
}

.social-icon:hover {
  transform: translateY(-3px);
}

.wechat {
  background-color: #07c160;
  border-color: #07c160;
  color: white;
}

.weibo {
  background-color: #e6162d;
  border-color: #e6162d;
  color: white;
}

.github {
  background-color: #24292e;
  border-color: #24292e;
  color: white;
}

/* 响应式样式 */
@media (max-width: 768px) {
  .login-box {
    flex-direction: column;
    width: 90%;
    max-width: 400px;
  }
  
  .welcome-section {
    padding: 30px;
  }
  
  .welcome-content {
    text-align: center;
  }
  
  .logo {
    margin-bottom: 20px;
  }
  
  .welcome-title {
    font-size: 24px;
  }
  
  .welcome-image {
    display: none;
  }
  
  .form-section {
    padding: 30px;
  }
}
</style> 